package com.guan.core.entity;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 封装页面的参数
 * 页面数据封装
 * Created by Administrator on 2018/1/25 0025.
 */
public class PageData extends  HashMap implements Map{
    private Map<String,Object> map = null;
    private HttpServletRequest request;
    public PageData(){
        map = new HashMap<String,Object>();
    }
    public PageData(HttpServletRequest request){
        this.request = request;
        Map reqs =  request.getParameterMap();
        map = new HashMap<String,Object>();
        Iterator<String> iterator = reqs.keySet().iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            Object o = reqs.get(key);
            if(null == o){
                map.put(key,"");
            }else if(o instanceof String[]){
                String v = "";
                String[] vs = (String[])o;
                for(int i =0;i<vs.length;i++){
                    v+=vs[i]+",";
                }
                map.put(key,v.substring(0,v.length()-1));
            }else{
                map.put(key,o.toString());
            }
        }
    }

    /**
     * 判断存不存在这个key
     * @param key
     * @return
     */
    public boolean hasKey(String key){
        return  map.containsKey(key);
    }

    /**
     * 设置值
     * @param re
     */
    public void setData(Map<String,Object> re){
        this.map = re;
    }
    public Object get(String key){
        if(map ==null){
            return  null;
        }else {
            return map.get(key);
        }
    }
    public void put(String key,String vlalue){
        map.put(key,vlalue);
    }
    public String getString(String key){
        Object o = map.get(key);
        if(null == o){
            return  "";
        }else{
            return o.toString();
        }
    }
}
